package com.dao.airticle;

import com.mapper.ArticleMapper;
import com.pojo.Article;
import com.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;

public class PublishBlog {

       /**
        *发布博客
        */
       public static String publish_blog(Article article) throws IOException {
              System.out.println("[info]发布博客");
              //获取sqlSession对象
              SqlSession sqlSession= MybatisUtil.getSqlSession();
              //3.获取Mapper接口的代理对象
              ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
              //4.执行方法
              articleMapper.publish_blog(article);
              //提交事务
              sqlSession.commit();
              //5.释放资源
              sqlSession.close();

              return article.getId();
       }
       /**
        * 保存草稿
        */
       public static String save_draft(Article article)throws IOException{
              System.out.println("[info]保存草稿");
              //获取sqlSession对象
              SqlSession sqlSession= MybatisUtil.getSqlSession();
              //3.获取Mapper接口的代理对象
              ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
              //4.执行方法
              articleMapper.publish_blog(article);
              //提交事务
              sqlSession.commit();
              //5.释放资源
              sqlSession.close();

              return article.getId();
       }

       /**
        * 存入博客的标签信息
        */
       public static void publish_blog_label(String[] label_list,String articleId)throws  IOException{
              System.out.println("[info]存入博客的标签");
              //获取sqlSession对象
              SqlSession sqlSession= MybatisUtil.getSqlSession();
              //3.获取Mapper接口的代理对象
              ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
              //4.执行方法
              for (String s : label_list) {
                     articleMapper.insert_label(s, articleId);
                     //提交事务
                     sqlSession.commit();
              }
              //5.释放资源
              sqlSession.close();
       }

       /**
        * 存入博客的专栏信息
        */
       public static void publish_blog_column(String[] column_list,String articleId)throws  IOException{
              System.out.println("[info]存入博客的专栏");
              //获取sqlSession对象
              SqlSession sqlSession= MybatisUtil.getSqlSession();
              //3.获取Mapper接口的代理对象
              ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
              //4.执行方法
              for (String s : column_list) {
                     articleMapper.insert_column(s, articleId);
                     //提交事务
                     sqlSession.commit();
              }
              //5.释放资源
              sqlSession.close();
       }

       /**
        *
        * 获取博客内容
        */
       public static Article get_blog_content(String blog_id)throws IOException{
              System.out.println("[info]获取博客内容");
              //获取sqlSession对象
              SqlSession sqlSession= MybatisUtil.getSqlSession();
              //3.获取Mapper接口的代理对象
              ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
              //4.执行方法
              Article article=articleMapper.get_blog_content(blog_id);
              //提交事务
              sqlSession.commit();
              //5.释放资源
              sqlSession.close();
              return article;
       }
}
